local Tabledump = require("Tabledump")
local BattleMap = require("BattleMap")
local BattleType = require("BattleConst").BattleType
-- Enemy.lua

-- 敌人类
local Enemy = {}
Enemy.__index = Enemy
local battleMap = {}
battleMap = BattleMap.new(self,BattleType.COOP) 
function Enemy:new(side,enmeyType,enmeyid, health, speed)
    -- Tabledump.dump( "Enemy:new->",path[1])
    local enemy = {
        x = 0,
        y = 0,
        health = health, --生命值
        alive = true,   --是否活着
        speed = speed,-- 移动速度
        side = side,    --那边的敌人 1是上边 2是下边
        enmeyType = enmeyType, --敌人种类
        enmeyid = enmeyid, --敌人种类
        distance =0,    --移动距离
        originalSpeed = speed, -- 记录原始速度
        -- slowFactor = 0.5, -- 减速因子
        -- slowDuration = 2, -- 减速持续时间
        -- slowTimer = 0, -- 计时器
        slowEffects = {}, -- 存储减速效果
        deadbyside=0    --那边杀死的
    }
    setmetatable(enemy, Enemy)
    return enemy
end

function Enemy:update(dt)
    if not self.alive then return end
       -- 处理减速逻辑
    -- if self.slowTimer > 0 then
    --     self.slowTimer = self.slowTimer - dt
    --     if self.slowTimer <= 0 then
    --         self.speed = self.originalSpeed -- 恢复原始速度
    --     end
    -- end
    
    for i = #self.slowEffects, 1, -1 do
        local effect = self.slowEffects[i]
        effect.duration = effect.duration - dt
        
        if effect.duration <= 0 then
            table.remove(self.slowEffects, i) -- 移除过期的减速效果
        end
    end
    self.distance = self.distance + self.speed * dt
    local t = battleMap:getPosInfoByLength(self.side, self.distance)
    self.x = t.pos.x
    self.y = t.pos.y
    -- Tabledump.dump( "Enemy:update->", self.x, self.y)
end
function Enemy:applySlow()
    self.speed = self.originalSpeed * self.slowFactor -- 减速
    self.slowTimer = self.slowDuration -- 重置减速计时器
end
-- 减速效果结构
function Enemy:applySlowEffect(duration, slow,slow_global)
    -- duration  持续时间
    -- slow  减速比例 20%
    local effect = {
        duration = duration,
        slow = slow
    }
    table.insert(self.slowEffects, effect)

    local finalSlowFactor = 1.0
    -- 实现多个减速效果的叠加和计算。
    -- 计算总减速系数
    for _, slowEffect in ipairs(self.slowEffects) do
        finalSlowFactor = finalSlowFactor * (1 - slowEffect.slow)
    end
    if slow_global then
        finalSlowFactor = finalSlowFactor * (1 - slow_global)
    end
    -- 计算最终速度
    local finalSpeed = self.originalSpeed * finalSlowFactor

    
    self.speed = finalSpeed -- 减速
end


return Enemy